﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Microsoft.Maps.MapControl;

namespace imaginecup
{
    public partial class MainPage : UserControl
    {
        private int state; 
        private int initStatus = 0;
        private int choosePointStatus = 1;
        private int chooseRegionStatus = 2;
        private ServiceReference1.DataProviderSoapClient getHotListClient = new imaginecup.ServiceReference1.DataProviderSoapClient();
        private List<ServiceReference1.CPosition> hotlist;


        public MainPage()
        {
            InitializeComponent();

            state = initStatus;

            Public.p = this;

            btnChoosePoint.Click += new RoutedEventHandler(btnChoosePoint_Click);
            btnShowMessage.Click += new RoutedEventHandler(btnShowMessage_Click);
            btnShowPhoto.Click += new RoutedEventHandler(btnShowPhoto_Click);
            btnShowPoint.Click += new RoutedEventHandler(btnShowPoint_Click);
            btnSelectRegion2.Click += new RoutedEventHandler(btnSelectRegion2_Click);
            PolluteCombo.SelectionChanged += new SelectionChangedEventHandler(PolluteCombo_SelectionChanged);

            //gethotList
            lbHotList.SelectionChanged += new SelectionChangedEventHandler(lbHotList_SelectionChanged);
            getHotListClient.getHotListCompleted += new EventHandler<imaginecup.ServiceReference1.getHotListCompletedEventArgs>(getHotListClient_getHotListCompleted);
            getHotList();
        }

        

        

        void btnSelectRegion2_Click(object sender, RoutedEventArgs e)
        {
            SearchPlaceWin swin = new SearchPlaceWin();
            swin.Title = "搜索你要标注的区域";
            swin.Content = new SearchPlace();
            swin.Show();
        }


        void btnShowPoint_Click(object sender, RoutedEventArgs e)
        {
            pmap.AddPointLayer();
        }

        void PolluteCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            
            string chooseitem = ((ListBoxItem)(cb.SelectedItem)).Content.ToString();
            switch (chooseitem)
            {
                case "综合评价":
                    pmap.AddPollutedLayer(0);
                    break;
                case "水污染":
                    pmap.AddPollutedLayer(2);
                    break;
                case "大气污染":
                    pmap.AddPollutedLayer(1);
                    break;
                case "土壤污染":
                    pmap.AddPollutedLayer(3);
                    break;
                case "噪声污染":
                    pmap.AddPollutedLayer(4);
                    break;
                default:
                    throw new Exception("未遇到的污染");
            }
        }

        /// <summary>
        /// 显示当前区域内的图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnShowPhoto_Click(object sender, RoutedEventArgs e)
        {
            //获取当前地图的东西南北四个坐标位置
            //获取此区域内的所有照片信息
            //通过添加一个ShowPhotoControl来显示图片
            ShowPhotoWin photowin = new ShowPhotoWin();
            photowin.Title = "显示本地区的照片";
            photowin.Content = new ShowPhoto(pmap.testmap.BoundingRectangle.East, pmap.testmap.BoundingRectangle.West, pmap.testmap.BoundingRectangle.North, pmap.testmap.BoundingRectangle.South);
            photowin.Show();
        }

        /// <summary>
        /// 显示当前区域的留言信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnShowMessage_Click(object sender, RoutedEventArgs e)
        {
            //获取当前地图的东西南北四个坐标位置

            //获取此区域内的所有留言信息

            //通过添加一个ShowMessageControl来显示图片
            ShowMessageWin messagewin = new ShowMessageWin();
            messagewin.Title = "显示本地区的留言";
            messagewin.Content = new ShowMessage(pmap.testmap.BoundingRectangle.East, pmap.testmap.BoundingRectangle.West, pmap.testmap.BoundingRectangle.North, pmap.testmap.BoundingRectangle.South);
            messagewin.Show();
        }

        /// <summary>
        /// 此鼠标点击后说明当前状态为选择点
        /// 选择此状态后左键单击后会弹出添加污染记录的usercontrol
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnChoosePoint_Click(object sender, RoutedEventArgs e)
        {
            pmap.testmap.MouseClick += new EventHandler<Microsoft.Maps.MapControl.MapMouseEventArgs>(testmap_MouseClick_ChoosePoint);
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 选择点并添加记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void testmap_MouseClick_ChoosePoint(object sender, Microsoft.Maps.MapControl.MapMouseEventArgs e)
        {
            //移除事件，恢复状态
            pmap.testmap.MouseClick -= new EventHandler<Microsoft.Maps.MapControl.MapMouseEventArgs>(testmap_MouseClick_ChoosePoint);
            state = 0;

            //如果level过小，不予处理
            if (pmap.testmap.ZoomLevel < 3)
            {
                MessageBox.Show("请放大地图级别在精确的选择要评价的地区");
                return;
            }

            //获取位置
            Location l = getLocationFromPoint(e.ViewportPoint);
            debugText.Text = "lat:" + l.Latitude + "long:" + l.Longitude;

            //弹出添加污染信息的control
            

            AddRecordWin tmpwin = new AddRecordWin();
            tmpwin.Title = "您标注的位置经度："+l.Longitude+"纬度："+l.Latitude;
            tmpwin.Content = new AddRecord(l,tmpwin);
            tmpwin.Show();
            


            //throw new NotImplementedException();
        }

        /// <summary>
        ///  选择一个box
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void testmap_MouseDragBox_ChooseRegion(object sender, Microsoft.Maps.MapControl.MapMouseDragEventArgs e)
        {
            

            //throw new NotImplementedException();
        }

        /// <summary>
        /// 根据屏幕上一点的像素位置坐标得到经纬度坐标
        /// 在level 0 会有bug
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        private Location getLocationFromPoint(Point p)
        {
            
            double west = pmap.testmap.BoundingRectangle.West;
            double east = pmap.testmap.BoundingRectangle.East;
            double north = pmap.testmap.BoundingRectangle.North;
            double south = pmap.testmap.BoundingRectangle.South;

            double longtitude = p.X / pmap.testmap.Width * pmap.testmap.BoundingRectangle.Width + west;
            double latitude = north - p.Y / pmap.testmap.Height * pmap.testmap.BoundingRectangle.Height;

            return new Location(latitude, longtitude);
        }

        /// <summary>
        /// 获取当前热点信息
        /// </summary>
        private void getHotList()
        {
            getHotListClient.getHotListAsync();
        }

        void getHotListClient_getHotListCompleted(object sender, imaginecup.ServiceReference1.getHotListCompletedEventArgs e)
        {
            hotlist = new List<ServiceReference1.CPosition>(e.Result);
            for (int i = 0; i < hotlist.Count; i++)
            {
                lbHotList.Items.Add(hotlist[i].Name);
            }
        }

        void lbHotList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string str = e.AddedItems[0].ToString();
            int i;
            for (i = 0; i < hotlist.Count; i++)
            {
                if (hotlist[i].Name == str)
                {
                    break;
                }
            }

            if (i == hotlist.Count)
            {
                throw new Exception("error");
            }
            else
            {
                pmap.testmap.SetView(new Location(hotlist[i].Latitude, hotlist[i].Longtitude), hotlist[i].ZoomLevel);
            }
        }

    }
}
